/*
 * 2006-1-4 lxm 创建。 
 */
package com.hd123.rumba.organization;

import net.sf.hibernate.HibernateException;

import org.apache.axis.AxisFault;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.request.SessionedService;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.sql.ResultPage;

/**
 * 组织角色 | 管理员 | 数据交换类
 * 
 * @author lxm
 * 
 */
public class DOrganizationMgr extends SessionedService {

  public DOrganization getOrganization(String uuid) throws AxisFault {
    try {
      return toDOrganization(getOrganizationMgr().getOrganization(uuid));
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  public DOrganization getOrganizationByCode(String code) throws AxisFault {
    try {
      return toDOrganization(getOrganizationMgr().getOrganizationByCode(code));
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  public DOrganization getOrganizationByOrgDomain(String orgDomainUuid) throws AxisFault {
    try {
      return toDOrganization(getOrganizationMgr().getOrganizationByOrgDomain(orgDomainUuid));
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  public DOrganizationPage getTopOrganizations(int pageSize, int page) throws AxisFault {
    try {
      return toDOrganizationPage(getOrganizationMgr().getTopOrganizations(pageSize, page));
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  public DOrganizationPage getLowerOrganizations(String parentOrgUuid, int pageSize, int page)
      throws AxisFault {
    try {
      IOrganization parentOrg = getOrganizationMgr().getOrganization(parentOrgUuid);
      return toDOrganizationPage(getOrganizationMgr().getLowerOrganizations(parentOrg, pageSize,
          page));
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  private IOrganizationMgr orgMgr = null;

  private IOrganizationMgr getOrganizationMgr() throws BusinessException {
    if (orgMgr == null)
      orgMgr = (IOrganizationMgr) NamingMgr.getInstance().lookupMgr(IOrganizationMgr.class);
    return orgMgr;
  }

  private DOrganization toDOrganization(IOrganization org) {
    if (org == null)
      return null;
    DOrganization dorg = new DOrganization();
    dorg.assignFrom(org);
    return dorg;
  }

  private DOrganizationPage toDOrganizationPage(ResultPage rp) throws BusinessException,
      HibernateException {
    DOrganizationPage dop = new DOrganizationPage();
    dop.assignFrom(rp);
    return dop;
  }

}
